{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "09ae5c78",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer, DebertaV2ForMaskedLM\n",
    "from debertav2 import DebertaV2EmdForPreTraining\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9e573f2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "checkpoint-77000  checkpoint-78000  runs\r\n"
     ]
    }
   ],
   "source": [
    "!ls debertav2-base-malaysian-v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "509ee75f",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained('debertav2-base-malaysian-v2/checkpoint-78000')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ff92a357",
   "metadata": {},
   "outputs": [],
   "source": [
    "model =  DebertaV2EmdForPreTraining.from_pretrained(\n",
    "    'debertav2-base-malaysian-v2/checkpoint-78000',\n",
    "    torch_dtype = torch.bfloat16\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76fe5e8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6cbede90c3214ee296be15abeda002d5",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model.safetensors:   0%|          | 0.00/228M [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-debertav2-base/commit/2dc2cc3b3dddbba98ffaaac59baf2e3ca3968b8b', commit_message='Upload DebertaV2EmdForPreTraining', commit_description='', oid='2dc2cc3b3dddbba98ffaaac59baf2e3ca3968b8b', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.push_to_hub('mesolitica/malaysian-debertav2-base')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9489102c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-debertav2-base/commit/14a359fdf0068cbb2a052bf084449bcb9e90872b', commit_message='Upload tokenizer', commit_description='', oid='14a359fdf0068cbb2a052bf084449bcb9e90872b', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.push_to_hub('mesolitica/malaysian-debertav2-base')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "68468fb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"abang sedang[MASK] buku\"\n",
    "\n",
    "inputs = tokenizer(text, return_tensors=\"pt\")\n",
    "mask_token_index = torch.where(inputs[\"input_ids\"] == tokenizer.mask_token_id)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "be6df8fa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abang sedang membaca buku\n",
      "abang sedangع buku\n",
      "abang sedangoal buku\n",
      "abang sedang தட buku\n",
      "abang sedangal buku\n",
      "abang sedang إ buku\n",
      "abang sedang menjaga buku\n",
      "abang sedangட buku\n",
      "abang sedang. buku\n",
      "abang sedang memberitahu buku\n"
     ]
    }
   ],
   "source": [
    "# ORIGINAL\n",
    "\n",
    "logits = model(**inputs).logits\n",
    "mask_token_logits = logits[0, mask_token_index, :]\n",
    "top_3_tokens = torch.topk(mask_token_logits, 10, dim=1).indices[0].tolist()\n",
    "\n",
    "for token in top_3_tokens:\n",
    "    print(text.replace(tokenizer.mask_token, tokenizer.decode([token])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f11554e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
